--[[
-- Extension for Tidy Plates - Threat Plates for DarkRage guild use.
-- Features:
-- * Fix localization issues with unique mobs functions in Threat Plates
--   that enables "Lich King", "Drudge Ghoul", "Val'kyr Shadowguard" and
--   "Raging Spirit" for zhTW.
-- * Change the visual style for "Val'kyr Shadowguard" to "Drudge Ghoul" when
--   its health goes below 50%.
]]

TidyPlates_DarkRage = CreateFrame("frame")
TidyPlates_DarkRageDB = TidyPlates_DarkRageDB or {
  valky_mark_scale = 1.5
}
local DR = TidyPlates_DarkRage
local DRDB = TidyPlates_DarkRageDB
DR:SetScript("OnEvent", function(self, event, ...)
    self[event](self, event, ...) end)

DR.Locales = {}
DR.UniqueLocales = {}
local L = DR.Locales
local UL = DR.UniqueLocales

local function MyPrint(obj, prefix)
  if not prefix then prefix = "" end
  if type(obj) == "table" or type(obj) == "userdata" then
    print(prefix .. "table: <<")
    new_prefix = prefix .. "  "
    for i, v in pairs(obj) do
      if type(v) ~= "table" then
        print(new_prefix .. "[" .. i .. "] = " .. tostring(v))
      else
        print(new_prefix .. "[" .. i .. "] = ")
        MyPrint(v, new_prefix)
      end
    end
    print(prefix .. "<<")
  else
    print(prefix .. tostring(obj))
  end
end

-- Fixes localization. Only applicable for Threat Plates v3.1 beta.
local function FixUniqueLocalization()
  if GetLocale() == "enUS" then return end
  if TPuniqueList then
    for english_name, local_name in pairs(UL) do
      -- print("insert " .. local_name .. " as " .. TPuniqueList[english_name])
      TPuniqueList[local_name] = TPuniqueList[english_name]
    end
  end
end

-- Returns true if the instance is in heroic mode, and Vak'kyr has less health
-- than 50%.
local function IsHeroicUpperValkyr(unit)
  return unit.isHeroicValkyr and unit.health
      and unit.healthmax and unit.health * 2 < unit.healthmax
end

local function UpdateUnitStatic(extended)
  local unit = extended.unit

  unit.isValkyr = (unit.name == UL["Val'kyr Shadowguard"])
  if unit.isValkyr then
    local _, instanceType, difficulty, _, _, playerDifficulty,
        isDynamicInstance = GetInstanceInfo()
    unit.isHeroicValkyr = isDynamicInstance and instanceType == "raid"
        and playerDifficulty == 1
  end
end

local raidIconTexCoord = {
  [0] = {
    [0] = 1, --star
    [0.25] = 5, --moon
  },
  [0.25] = {
    [0] = 2, --circle
    [0.25] = 6, --square
  },
  [0.5] = {
    [0] = 3, --diamond
    [0.25] = 7, --cross
  },
  [0.75] = {
    [0] = 4, --triangle
    [0.25] = 8, --skull
  },
}

local function UpdateUnitDynamic(extended, unit)
  if unit.isValkyr and not unit.mark and unit.isMarked then
    local x, y = extended.regions.raidicon:GetTexCoord()
    if x and y then
      unit.mark = raidIconTexCoord[x][y] or 0
    end
  end
end

-- Hooks the update function, so we can change the scale/alpha/color of name
-- plates.
local function HookThemeSetup()
  if not TidyPlatesThemeList then return end
  local setup = TidyPlatesThemeList["Threat Plates"]
  if not setup then return end

  local SetScaleHooked = setup.SetScale
  local SetAlphaHooked = setup.SetAlpha
  local SetHealthbarColorHooked = setup.SetHealthbarColor
  local OnInitializeHooked = setup.OnInitialize
  local OnUpdateHooked = setup.OnUpdate

  -- Hack here, use Drudge Ghoul profile for upper val'kyr.
  local upper_val_id = TPuniqueList[UL["Drudge Ghoul"]]

  setup.SetScale = function (unit)
    if IsHeroicUpperValkyr(unit) then
      local db = TidyPlatesThreat.db.profile
      local uS = db.uniqueSettings[upper_val_id]
      return uS[7]
    end
    if unit.isValkyr and unit.mark and unit.mark == DRDB.valkyr_mark then
      return SetScaleHooked(unit) * (DRDB.valky_mark_scale or 1)
    end
    return SetScaleHooked(unit)
  end

  setup.SetAlpha = function (unit)
    if IsHeroicUpperValkyr(unit) then
      local db = TidyPlatesThreat.db.profile
      local uS = db.uniqueSettings[upper_val_id]
      return uS[8], db.blizzFade.toggle
    end
    return SetAlphaHooked(unit)
  end

  setup.SetHealthbarColor = function (unit)
    if IsHeroicUpperValkyr(unit) then
      local db = TidyPlatesThreat.db.profile
      local uS = db.uniqueSettings[upper_val_id]
      if uS[2] then
        return uS[3], uS[4], uS[5]
      else
        return unit.red, unit.green, unit.blue
      end
    end
    return SetHealthbarColorHooked(unit)
  end

  setup.OnInitialize = function (extended)
    UpdateUnitStatic(extended)
    OnInitializeHooked(extended)
  end

  setup.OnUpdate = function (extended, unit)
    UpdateUnitDynamic(extended, unit)
    OnUpdateHooked(extended, unit)
  end
end

function DR:ADDON_LOADED(event, addon)
  if (addon == "TidyPlates_DarkRage") then
    DR:UnregisterEvent("ADDON_LOADED")
    FixUniqueLocalization()
    HookThemeSetup()
    print(L.WELCOME)
  end
end
DR:RegisterEvent("ADDON_LOADED")
